﻿// -----------------------------------------------------------------------
// <copyright file="BootyManager.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace MenuGame2DXNA
{
    using System;
    using System.Collections.Generic;
    using System.Xml;
    using Microsoft.Xna.Framework.Content;
    using Microsoft.Xna.Framework;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class Tiles : VisibleGameEntity
    {
        private  short[,] _cellFlags;

        private short[,] _obstructions;

        public int NumberRows { get; set; }

        public int NumberCols { get; set; }

        public Vector2 SizeCell { get; set; }


        private readonly short _type;

        private  List<string> _strTextures;
        public Tiles(short type)
        {
             _type = type;
        }
        public Tiles(string fileNameXml, short type)
        {
            
            _type = type;
            //LoadMapCell From file
            var xdomBooty = new XmlDocument();
            xdomBooty.Load(fileNameXml);
            InitFromXml(xdomBooty);
            
        }

        public void InitFromXml(XmlDocument xdomBooty)
        {
            var texturesMapCell = (XmlElement)xdomBooty.SelectSingleNode("Tiles/textures");
            _strTextures = new List<string>();
            if (texturesMapCell != null)
                foreach (XmlElement strTexture in texturesMapCell.ChildNodes)
                {

                    _strTextures.Add(strTexture.InnerText);

                }
            var xEnRows = (XmlElement)xdomBooty.SelectSingleNode("Tiles/nRows");
            if (xEnRows != null) NumberRows = int.Parse(xEnRows.InnerText);

            var xEnCols = (XmlElement)xdomBooty.SelectSingleNode("Tiles/nCols");
            if (xEnCols != null) NumberCols = int.Parse(xEnCols.InnerText);

            int HightCell = 0;
            int WidthCell = 0;
            var xeHeight = (XmlElement)xdomBooty.SelectSingleNode("Tiles/Height");
            if (xeHeight != null) HightCell = int.Parse(xeHeight.InnerText);

            var xeWidth = (XmlElement)xdomBooty.SelectSingleNode("Tiles/Width");
            if (xeWidth != null) WidthCell = int.Parse(xeWidth.InnerText);

            SizeCell = new Vector2(WidthCell, HightCell);
            Size = new Vector2(SizeCell.X * NumberCols, SizeCell.Y * NumberRows);

            //LoadMatrix From file
            var xeMatrixCell = (XmlElement)xdomBooty.SelectSingleNode("Tiles/Matrix");
            if (xeMatrixCell != null)
            {
                string[] rowMatrix = xeMatrixCell.InnerText.Split(';');
                _cellFlags = new short[NumberRows, NumberCols];
                for (var i = 0; i < NumberRows; i++)
                {
                    string[] cols = rowMatrix[i].Split(',');
                    for (int j = 0; j < cols.Length; j++)
                    {
                        _cellFlags[i, j] = short.Parse(cols[j]);

                    }
                }
            }
        }
        public Tiles(string fileNameXml, short type, Tiles tiles,Point limitRanDom,short valueRandom)
        {
           
            _type = type;
            //LoadMapCell From file
            var xdomBooty = new XmlDocument();
            xdomBooty.Load(fileNameXml);

            limitRanDom = InitFromXmlRandom(tiles, limitRanDom, valueRandom, xdomBooty);
            

        }

        public Point InitFromXmlRandom(Tiles tiles, Point limitRanDom, short valueRandom, XmlDocument xdomBooty)
        {
            var texturesMapCell = (XmlElement)xdomBooty.SelectSingleNode("Tiles/textures");
            _strTextures = new List<string>();
            if (texturesMapCell != null)
                foreach (XmlElement strTexture in texturesMapCell.ChildNodes)
                {
                    _strTextures.Add(strTexture.InnerText);
                }
            var xEnRows = (XmlElement)xdomBooty.SelectSingleNode("Tiles/nRows");
            if (xEnRows != null) NumberRows = int.Parse(xEnRows.InnerText);

            var xEnCols = (XmlElement)xdomBooty.SelectSingleNode("Tiles/nCols");
            if (xEnCols != null) NumberCols = int.Parse(xEnCols.InnerText);

            int HightCell = 0;
            int WidthCell = 0;
            var xeHeight = (XmlElement)xdomBooty.SelectSingleNode("Tiles/Height");
            if (xeHeight != null) HightCell = int.Parse(xeHeight.InnerText);

            var xeWidth = (XmlElement)xdomBooty.SelectSingleNode("Tiles/Width");
            if (xeWidth != null) WidthCell = int.Parse(xeWidth.InnerText);

            SizeCell = new Vector2(WidthCell, HightCell);

            _cellFlags = new short[NumberRows, NumberCols];

            var random = new Random();
            for (var i = 0; i < NumberRows; i++)
                for (var j = 0; j < NumberCols; j++)
                {
                    if (tiles[new Point(i, j)] == valueRandom)
                        _cellFlags[i, j] = (short)random.Next(limitRanDom.X, limitRanDom.Y);
                }
            return limitRanDom;
        }

        public void Init(ContentManager contentManager,short[,] obstructions,short limit)
        {
            _obstructions = obstructions;
            ObjectVisible = new Cell[NumberRows, NumberCols];
            for (var i = 0; i < NumberRows; i++)
            {
                for (var j = 0; j < NumberCols; j++)
                {
                    if (_cellFlags[i, j] > limit)
                        _obstructions[i, j] += _type;
                    ObjectVisible[i, j] = new Cell(contentManager, _strTextures[_cellFlags[i, j]], new Vector2(j * SizeCell.X, i * SizeCell.Y), new Vector2(0, 0));
                }
            }
        }
        public int RemoveCell(Point pIndex)
        {
            int result  = 0;
            if(_cellFlags[pIndex.X,pIndex.Y] > 0)
            {
                result = _cellFlags[pIndex.X, pIndex.Y];

                _cellFlags[pIndex.X, pIndex.Y] = 0;
                
                _obstructions[pIndex.X, pIndex.Y]-= _type;

            }
            return result;
        }
        public short this[Point pIndex]    // Indexer declaration
        {
            get
            {
                return _cellFlags[pIndex.X, pIndex.Y];
            }
            set
            {
                _cellFlags[pIndex.X, pIndex.Y] = value;
            }
        }
        public override void Draw(GameTime gameTime, Microsoft.Xna.Framework.Graphics.SpriteBatch spriteBatch)
        {

            for (int i = 0; i < NumberRows; i++)
            {
                for (int j = 0; j < NumberCols; j++)
                {
                    
                    ObjectVisible[i, j].TopLeft = new Vector2(j * SizeCell.X + TopLeft.X, i * SizeCell.Y + TopLeft.Y);
                    if (_cellFlags[i, j] != 0)
                    {
                        ObjectVisible[i, j].Draw(gameTime, spriteBatch);
                    }
                }
            }
            base.Draw(gameTime, spriteBatch);
        }
    }
}
